<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会话ID" prop="chat_id">
        <el-input-number :controls="false"
          v-model="queryParams.chat_id"
          type="number"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="搜索内容" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID"  width="55" align="center" prop="id" />
      <el-table-column label="对话ID"  width="85" align="center" prop="chat_id" :show-overflow-tooltip="true" />
      <el-table-column label="URL" align="center" prop="url" :show-overflow-tooltip="true" />
      <el-table-column label="请求体" align="center" prop="request_body" :show-overflow-tooltip="true" />
      <el-table-column label="响应体" align="center" prop="response_body" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="created_at" width="180" :formatter="formatterTimeYmdHm"/>
      <el-table-column label="更新时间" align="center" prop="updated_at" width="180" :formatter="formatterTimeYmdHm"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog title="日志详情" :visible.sync="detailVisible" width="90%" append-to-body :close-on-click-modal="false">
      <el-tabs type="border-card">
        <el-tab-pane label="请求体">
          {{ void (tab1 = 'jv', tab2 = 'jv') }}
          <el-tabs type="card" v-model="tab1">
            <el-tab-pane name="str" label="字符内容">
              <el-input type="textarea" readonly :rows="25" v-model="form.request_body" />
            </el-tab-pane>
            <el-tab-pane name="pre"  label="pre-json块">
              <pre style="white-space: pre-wrap;">{{ formatJson(form.request_body) }}</pre>
            </el-tab-pane>
            <el-tab-pane name="jv"  label="请求体jsonViewer">
              <json-viewer
                :preview-mode="false"
                :expand-depth="5"
                copyable
                :show-array-index="false"
                theme="my-awesome-json-theme"
                v-if="form.request_body !== null"
                :value="formatJson(form.request_body)">
                <template v-slot:copy>
                  <el-link :underline="false" icon="el-icon-document-copy" v-clipboard:copy="form.request_body" v-clipboard:success="clipboardSuccess" style="float:right">复制</el-link>
                </template>
              </json-viewer>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
        <el-tab-pane label="响应体">
          {{ void (tab1 = 'jv', tab2 = 'jv') }}
          <el-tabs type="card" v-model="tab1">
            <el-tab-pane name="str" label="字符内容">
              <el-input type="textarea" readonly :rows="25" v-model="form.response_body" />
            </el-tab-pane>
            <el-tab-pane name="pre"  label="pre-json块">
              <pre style="white-space: pre-wrap;">{{ formatJson(form.response_body) }}</pre>
            </el-tab-pane>
            <el-tab-pane name="jv"  label="请求体jsonViewer">
<!--              :copyable="{ copyText: '复制', copiedText: '已复制' }"-->
              <json-viewer
                :preview-mode="false"
                :expand-depth="5"
                copyable
                :show-array-index="false"
                theme="my-awesome-json-theme"
                v-if="form.response_body !== null"
                :value="formatJson(form.response_body)">
                <template v-slot:copy>
                  <el-link :underline="false" icon="el-icon-document-copy" v-clipboard:copy="form.response_body" v-clipboard:success="clipboardSuccess" style="float:right">复制</el-link>
                </template>
              </json-viewer>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import { listOpenaiLog, delOpenaiLog } from '@/api/openai-log'
import JsonViewer from 'vue-json-viewer'

export default {
  name: "OpenaiLog",
  components:{ JsonViewer },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日志列表数据
      mainList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chat_id: undefined,
        searchValue: undefined,
      },
      // 详情对话框可见性
      detailVisible: false,
      // 当前查看的日志详情数据
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询日志列表 */
    getList() {
      this.loading = true;
      listOpenaiLog(this.queryParams).then(response => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.mainList = [];
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 查看详情 */
    handleView(row) {
      this.form = row;
      this.detailVisible = true;
    },
    /** 格式化 JSON 数据 */
    formatJson(json) {
      try {
        // return JSON.stringify(JSON.parse(json), null, 2);
        return JSON.parse(json);
      } catch (e) {
        return json;
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(() => {
        return delOpenaiLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 复制代码成功 */
    clipboardSuccess() {
      this.$modal.msgSuccess("复制成功");
    },
  }
};
</script>

<style scoped>
/*
values are default one from jv-light template
*/
.my-awesome-json-theme {
  background: #fff;
  white-space: nowrap;
  color: #525252;
  font-size: 14px;
  font-family: Consolas, Menlo, Courier, monospace;

  .jv-ellipsis {
    color: #999;
    background-color: #eee;
    display: inline-block;
    line-height: 0.9;
    font-size: 0.9em;
    padding: 0px 4px 2px 4px;
    border-radius: 3px;
    vertical-align: 2px;
    cursor: pointer;
    user-select: none;
  }
  .jv-button { color: #49b3ff }
  .jv-key { color: #111111 }
  .jv-item {
    &.jv-array { color: #111111 }
    &.jv-boolean { color: #fc1e70 }
    &.jv-function { color: #067bca }
    &.jv-number { color: #fc1e70 }
    &.jv-number-float { color: #fc1e70 }
    &.jv-number-integer { color: #fc1e70 }
    &.jv-object { color: #111111 }
    &.jv-undefined { color: #e08331 }
    &.jv-string {
      color: #42b983;
      word-break: break-word;
      white-space: normal;
    }
  }
  .jv-code {
    .jv-toggle {
      &:before {
        padding: 0px 2px;
        border-radius: 2px;
      }
      &:hover {
        &:before {
          background: #eee;
        }
      }
    }
  }
}

</style>
